############################# S4 DEMOGRAPHICS TABLE, FFRISP #####################################

detach(data1)
data2 <- read.spss("Wave4_JAN_09_with_demographics.sav")
attach(data2)

write.csv(attributes(data2)$variable.labels, "ffrispvarsdemog.csv")

weights2 <- data2$finalwgt/mean(data2$finalwgt, na.rm=TRUE)
weights2[w4==1] <- data2$finalwgt[w4==1]/mean(data2$finalwgt[w4==1], na.rm=TRUE)

age <- PS_HHCOMP1_Yourself_GV1
age[is.na(age)] <- PS_Correct_Age[is.na(age)]
age[is.na(age)] <- 2008-PS_BirthDateYear[is.na(age)]

#PS_HHCOMP1_Yourself_GV1_Codes
female <- PS_HHCOMP1_Yourself_GV2=="Female"
male <- PS_HHCOMP1_Yourself_GV2=="Male"
female[PS_HHCOMP1_Yourself_GV2=="Please Select"] <- NA
male[PS_HHCOMP1_Yourself_GV2=="Please Select"] <- NA
female[is.na(female)] <- PS_HHCOMP2_Yourself_GV2[is.na(female)]=="Female"
male[is.na(male)] <- PS_HHCOMP2_Yourself_GV2[is.na(male)]=="Male"

ps <- function(x){
  name <- c(names(table(x)), "n")
  n <- sum(table(x))
  t <- table(x)/n
  out <- c(t, n)
  names(out) <- name
  out
}

wt <- function(x){
  one <- wtd.table(x[w4==1], weights2[w4==1])$sum.of.weights
  two <- one/sum(one)
  list(one, two, sum(one))
}

ps(male)

# Column 10: Without Post-Stratification
sum(table(age[age<25 & w4==1]))/sum(table(age[w4==1]))
sum(table(age[age>24 & age<35 & w4==1]))/sum(table(age[w4==1]))
sum(table(age[age>34 & age<45 & w4==1]))/sum(table(age[w4==1]))
sum(table(age[age>44 & age<55 & w4==1]))/sum(table(age[w4==1]))
sum(table(age[age>54 & age<65 & w4==1]))/sum(table(age[w4==1]))
sum(table(age[age>64 & w4==1]))/sum(table(age[w4==1]))
wt(age)

table(PS_Q1401[w4==1])/sum(table(PS_Q1401[w4==1])) #White
table(PS_Q1402)/sum(table(PS_Q1402))
table(hispanic[w4==1])/sum(table(hispanic[w4==1]))
wt(hispanic)

w4 <- as.numeric(w4_STATUS=="complete            ")

# Gender
table(female[w4==1])/sum(table(female[w4==1]))
sum(table(female[w4==1]))
wt(female)

#White
table(PS_Q1401[w4==1])/sum(table(PS_Q1401[w4==1]))
sum(table(PS_Q1401[w4==1]))
wt(PS_Q1401)

#Hispanic
table(hispanic[w4==1])/sum(table(hispanic[w4==1])) 
sum(table(hispanic[w4==1]))
wt(hispanic)

# Education
table(PS_Q15)

educ <- rep(NA, length(PS_Q15))
educ[PS_Q15=="No schooling completed"] <- "Less than High School"
educ[PS_Q15=="Nursery school to 4th grade"] <- "Less than High School"
educ[PS_Q15=="5th or 6th grade"] <- "Less than High School"
educ[PS_Q15=="7th or 8th grade"] <- "Less than High School"
educ[PS_Q15=="9th grade"] <- "Less than High School"
educ[PS_Q15=="10th grade"] <- "Less than High School"
educ[PS_Q15=="11th grade"] <- "Less than High School"
educ[PS_Q15=="12th grade NO DIPLOMA"] <- "Less than High School"
educ[PS_Q15=="HIGH SCHOOL GRADUATE high school DIPLOMA or the equivalent (GED)"] <- "High School Diploma"
educ[PS_Q15=="Some college, no degree"] <- "Some College"
educ[PS_Q15=="Associate degree"] <- "Some College"
educ[PS_Q15=="Bachelor's degree"] <- "College Graduate"
educ[PS_Q15=="Master's degree"] <- "Graduate Degree"
educ[PS_Q15=="Professional or Doctorate degree"] <- "Graduate Degree"

sum(table(educ[educ=="Less than High School" & w4==1]))/sum(table(educ[w4==1]))
sum(table(educ[educ=="High School Diploma" & w4==1]))/sum(table(educ[w4==1]))
sum(table(educ[educ=="Some College" & w4==1]))/sum(table(educ[w4==1]))
sum(table(educ[educ=="College Graduate" & w4==1]))/sum(table(educ[w4==1]))
sum(table(educ[educ=="Graduate Degree" & w4==1]))/sum(table(educ[w4==1]))
wt(educ)

# Column 11: Post-Stratification
wa <- sum(wtd.table(age[w4==1], weights2[w4==1])$sum.of.weights)

#Age
sum(wtd.table(age[age<25 & w4==1], weights2[age<25 & w4==1])$sum.of.weights)/wa
sum(wtd.table(age[age>24 & age<35 & w4==1], weights2[age>24 & age<35 & w4==1])$sum.of.weights)/wa
sum(wtd.table(age[age>34 & age<45 & w4==1], weights2[age>34 & age<45 & w4==1])$sum.of.weights)/wa
sum(wtd.table(age[age>44 & age<55 & w4==1], weights2[age>44 & age<55 & w4==1])$sum.of.weights)/wa
sum(wtd.table(age[age>54 & age<65 & w4==1], weights2[age>54 & age<65 & w4==1])$sum.of.weights)/wa
sum(wtd.table(age[age>64 & w4==1], weights2[age>64 & w4==1])$sum.of.weights)/wa

#Gender
sum(wtd.table(female[w4==1], weights2[w4==1])$sum.of.weights)/wa
wt(female)

#White
sum(wtd.table(PS_Q1401[w4==1])$sum.of.weights)/wa
wt(PS_Q1401)

#Hispanic
sum(wtd.table(hispanic[w4==1])$sum.of.weights)/wa
wt(hispanic)

#Education
sum(wtd.table(educ[educ=="Less than High School" & w4==1], weights2[educ=="Less than High School" & w4==1])$sum.of.weights)/wa
sum(wtd.table(educ[educ=="High School Diploma" & w4==1], weights2[educ=="High School Diploma" & w4==1])$sum.of.weights)/wa
sum(wtd.table(educ[educ=="Some College" & w4==1], weights2[educ=="Some College" & w4==1])$sum.of.weights)/wa
sum(wtd.table(educ[educ=="College Graduate" & w4==1], weights2[educ=="College Graduate" & w4==1])$sum.of.weights)/wa
sum(wtd.table(educ[educ=="Graduate Degree" & w4==1], weights2[educ=="Graduate Degree" & w4==1])$sum.of.weights)/wa
wt(educ)
